<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式-透明单例</title>
</head>

<body>
    <script>
        // 完成再页面上创建唯一的div
        var createDiv = (function () {
            var instance
            // 构造函数
            var createDiv = function (html) {
                if (instance) {
                    return instance
                }
                this.html = html
                this.init()
                return instance = this
            }
            //原型方法 创建div
            createDiv.prototype.init = function () {
                var div = document.createElement('div')
                div.innerHTML = this.html
                document.body.appendChild(div)
            }
            return createDiv
        })()
        // console.log(createDiv);
        let div1 = new createDiv(`<div>1111</div>`)
        div1.init()
        // 单例模式,只会出现第1i个
        let div2 = new createDiv(`<div>222</div>`)

        console.log(div1 === div2);
    </script>
</body>

</html>